Ext.define('Ext.ux.desktop.applications.Doctors', {
	extend: 'Ext.ux.desktop.applications.AbstractApplication',
	
	id:'doctors-app-window',
	title: 'Doctors',
	iCls: 'icon-doctors',
	//pageSize: 20,
	//grid: null,
	//dialogs: {},
	scriptURL: "/admin/doctors/",
	modelName: 'Doctors',
	//store: null,
/*
	init: function(){
		this.createWindow();
	},
	*/
	createModel: function(){
		Ext.define(this.modelName, {
		    extend: 'Ext.data.Model',
		    fields: [
		        {name: 'id', type: 'int'},
		        {name: 'f_name',  type: 'string'},
		        {name: 'l_name',  type: 'string'},
		        {name: 'specialty', type: 'string'}
		    ]
		});
	},
	
	createStore: function(){
		this.store =  new Ext.data.Store({
			model: this.modelName,
			proxy: {
		        type: 'ajax',
		        url : this.scriptURL + 'index',
		        reader: {
		        	type: 'json',
		        	root: 'data',
		        	idProperty: 'id',
		        	totalProperty: 'total'
		        }
		    },
		    pageSize: this.pageSize,
			remoteSort: false,
			autoLoad: true
		});
	},
	
	createWindow: function(){
		var _this = this;
		this.createModel();
		this.createStore();
		var window = Ext.create('Ext.window.Window',{
			id: this.id,
			title: this.title,
			iconCls: this.iCls,
			width:400,
			height:400,
			shim:false,
			animCollapse:false,
			constrainHeader:true,
			maximizable: false,
			layout: 'fit',
			items: [
				{
					xtype: 'grid',
					border: false,
					store: this.store,
					dockedItems: [{
					    xtype: 'toolbar',
					    dock: 'top',
					    id: this.id+'-grid-top-buttons',
					    items: [
                            new Ext.ux.form.SearchField({
                                store: this.store,
                                width: 150,
                                loadResult: function(){
                                    var ptbar = _this.grid.getDockedItems('.pagingtoolbar')[0];
                                    ptbar.moveFirst();
                                }
                            }),
					        {
					        	text: 'Add', 
					        	//iconCls: 'ico-add', 
					        	handler: function(){
					        		var d = this.getSubjectEditDialog();
									d.setMode('add');
					        		d.show({});
					        	},
								scope: this,
								disabled: false,
								btnName: 'add'
					        },{
					        	text: 'Edit', 
					        	//iconCls: 'ico-edit', 
					        	handler: function(){
					        		this.doRowAction('editSubject'); 
					        	},
					        	scope: this,
					        	disabled: true,
					        	btnName: 'edit'
					        },{
					        	text: 'Delete',
					        	//iconCls: 'ico-delete', 
					        	handler: function(){
					        		this.doRowAction('deleteSubject'); 
					        	},
					        	scope: this,
					        	disabled: true,
					        	btnName: 'delete'
					        }
					    ]
					}],
					columns:[
						{
							text: 'First Name',
							dataIndex: 'f_name'
						},
						{
							text: 'Last Name',
							dataIndex: 'l_name'
						},
						{
							text: 'Speciality',
							dataIndex: 'specialty'
						}
					],
					selModel: new Ext.selection.RowModel({
						mode: 'single',
						listeners: {
							selectionchange: function(sm) {
								var sc = sm.getCount();
                                /**
                                 * @todo delete sr variable
                                 */
								var sr = sm.getLastSelected();
								var bs = {
										'add': false,
										'edit': true,
										'delete': true
									};
								if (sc == 1){
									bs['edit'] = false;
									bs['delete'] = false;
								}
								this.setActiveButtons(this.grid.getDockedComponent(this.id+'-grid-top-buttons'),bs);
							},
							scope: this
						}
					}),
					forceFit:true,
					bbar: new Ext.PagingToolbar({
						pageSize: this.pageSize,
						store: this.store,
						displayInfo: true
					}),
					loadMask: true,
					listeners: {
						render: function(g) {
							this.grid = g;
						},
						scope: this
					}
				}
			]
		});
		window.show();
	},
	
	createEditDialog: function(){
		var dialogWindow = Ext.create('Ext.ux.desktop.modules.EditDialog',{
			id: 'doctors-app-edit-dialog',
			iconCls: this.iCls,
			items: {
				xtype: "form",
				bodyBorder: false,
				border: false,
				baseCls: "x-plain",
				style: "padding: 5px;",
				labelWidth: 110,
				url: '/admin/doctors/edit',
				items: [
					{xtype: "hidden", name: "id", dataIndex: "id"},
					{xtype:"textfield", fieldLabel: "First Name"+"*", name: "f_name", anchor: "99%", allowBlank: false},
					{xtype:"textfield", fieldLabel: "Last Name"+"*", name: "l_name", anchor: "99%", allowBlank: false},
					{xtype:"textfield", fieldLabel: "Speciality"+"*", name: "specialty", anchor: "99%", allowBlank: false}
				]
			},
			modeTitles: {
				add: 'Add Doctor',
				edit: 'Edit Doctor'
			},
			modeUrls: {
				add: this.scriptURL+'edit',
				edit: this.scriptURL+'edit'
			},
			listeners: {
				successSend: function(){
					if(this.grid) {
						this.grid.getStore().load();
					};
				},
				scope:this
			}
		});
		return dialogWindow;
	},
	
	doRowAction: function(action) {
		if (!action || !this.row_actions[action]) return false;
		var g = this.grid;
		if (!g) return false;
		var sm = g.getSelectionModel();
		var d = sm.getLastSelected();
		if (d && d.data) {
			this.row_actions[action].apply(this, [d.data, sm]);
		}
		return false;
	},
	
	row_actions: {
		editSubject:function(data, sm){
			var d = this.getSubjectEditDialog();
			d.setMode('edit');
			d.show(data);
		},
		
		deleteSubject:function(data, sm){
			var o = {
				sm: sm,
				grid: this.grid,
				url: this.scriptURL + 'delete',
				id_field: 'id'
			};
			Ext.applyIf(o, this.confirmation_obj);
			Ext.MessageBox.confirm('?','Do you really want to delete this doctor(s)?', o.onAccept, o);
		}
	},
	
	getSubjectEditDialog:function(){
		if (!this.dialogs['editDoctor']) {
			this.dialogs['editDoctor'] = this.createEditDialog();
		};
		return this.dialogs['editDoctor'];
	}
});